package com.mbl.base.modular.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mbl.base.config.security.SecurityUser;
import com.mbl.base.core.common.controller.BaseController;
import com.mbl.base.core.common.dto.MblResult;
import com.mbl.base.core.utils.UtilHttp;
import com.mbl.base.core.utils.UtilSecurity;
import com.mbl.base.modular.base.entity.Region;
import com.mbl.base.modular.base.service.IRegionService;
import com.mbl.base.modular.mall.entity.Address;
import com.mbl.base.modular.mall.service.IAddressService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author lwj
 * @since 2020-03-21
 */
@RestController
@RequestMapping("/mall/address")
public class AddressController extends BaseController<Address, IAddressService> {
    @Autowired
    private IRegionService regionService;

    @Override
    public void machiningList(QueryWrapper<Address> queryWrapper, String condition) {
        /**
         * 判断如果是官网用户
         * 则进行官网用户筛选
         */
        final SecurityUser user = UtilSecurity.getUser();
        if(user.getType() == 2){
            queryWrapper.eq("user_id", user.getId());
        } else {
            final String name = UtilHttp.getPara("name");
            queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
            final String userId = UtilHttp.getPara("userId");
            queryWrapper.eq(StringUtils.isNotBlank(userId), "user_id", userId);
        }
        super.machiningList(queryWrapper, condition);
    }

    @GetMapping("/setDefault")
    @ApiOperation("设置默认地址")
    public MblResult setDefault(Long id){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("user_id", UtilSecurity.getUser().getId());
        updateWrapper.set("is_default", false);
        getService().update(updateWrapper);
        final Address byId = getService().getById(id);
        byId.setIsDefault(true);
        getService().updateById(byId);
        return MblResult.success();
    }

    @Override
    public void machiningAdd(Address obj) {
        /**
         * 判断区域id不为空进行字段补充
         */
        final String areaCode = obj.getAreaCode();
        if(StringUtils.isNotBlank(areaCode)){
            final Region area = regionService.query().eq("code", areaCode).one();
            switch (area.getType()){
                case 1:
                    obj.setProvince(area.getName());
                    break;
                case 2:
                    obj.setCity(area.getName());
                    obj.setProvince(regionService.getById(area.getPid()).getName());
                    break;
                case 3:
                    obj.setCounty(area.getName());
                    final Region city = regionService.getById(area.getPid());
                    obj.setCity(city.getName());
                    final Region province = regionService.getById(city.getPid());
                    obj.setProvince(province.getName());
                    break;
            }
        }
        if(obj.getId() == null){
            obj.setUserId(UtilSecurity.getUser().getId());
        }
        super.machiningAdd(obj);
    }
}

